#!/bin/sh

SSD=nvme0n1p2
SSD_PATH=/dev/${SSD}
TIMEOUT=60
MOUNTPOINT=/media/hanweiai/opi_root

echo Start testing SSD:${SSD}

for i in `seq ${TIMEOUT}`;do
	echo Waiting for SSD inserted ... `expr ${TIMEOUT} - ${i}`
	sleep 1
	ls ${SSD_PATH} >/dev/null 2>&1 || continue

	echo SSD inserted...

	capacity=`cat /proc/partitions | grep ${SSD} -w | busybox awk '{printf $3}'`
	echo "${SSD}: ${capacity}"
	echo ${capacity} > /run/sd_capacity

	busybox mount | grep ${SSD_PATH} && exit 0

	mkdir -p ${MOUNTPOINT} 2>/dev/null

	# for p in `ls ${SSD_PATH}*`;do
	# 	echo Mounting ${p}...
	# 	busybox mount ${p} ${MOUNTPOINT} || continue

	# 	echo Mounted ${p}...
	# 	busybox umount ${MOUNTPOINT}
	# 	exit 0
	# done
	busybox mount ${SSD_PATH} ${MOUNTPOINT}
    if [ $? -eq 0 ]; then
        echo Mounted ${SSD_PATH}...
        exit 0
    else
        echo Failed to mount SSD:${SSD_PATH}...
        exit 1
    fi
done

echo Timed out waiting for SSD:${SSD}...
exit 2
